package com.lushu.chapitre1.graphics.controls
{
    import com.sfdk.graphics.core.SpriteEx;
    import com.sfdk.graphics.core.TextFieldEx;
    import com.sfdk.graphics.engine.data.CharacterDefinition;
    import com.sfdk.graphics.ui.parts.ComponentBase;
    import com.sfdk.locale.LocalisedMessageEngine;
    import com.sfdk.pools.BitmapDataPool;

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.events.Event;

    public class BattleInformation extends ComponentBase
    {
        private var _landEffect:SpriteEx;
        private var _landEffectContainer:Bitmap;
        private var _landEffectTF:TextFieldEx;
        private var _landEffectPercentTF:TextFieldEx;

        private var _characterInfoContainer:Bitmap;
        private var _characterInfoLeftTF:TextFieldEx;
        private var _characterInfoRightTF:TextFieldEx;
        private var _hpContainer:SpriteEx;
        private var _mpContainer:SpriteEx;

        private var _enemyInfo:SpriteEx;
        private var _enemyInfoContainer:Bitmap;
        private var _enemyInfoLeftTF:TextFieldEx;
        private var _enemyInfoRightTF:TextFieldEx;
        private var _enemyHPContainer:SpriteEx;
        private var _enemyMPContainer:SpriteEx;

        private var _currentCharacterDefinition:CharacterDefinition;
        private var _currentTileInfo:int = 15;
        private var _currentEnemyDefinition:CharacterDefinition;

        public function BattleInformation() {
            super();
        }

        override protected function createChildren():void {
            super.createChildren();

            _landEffect = new SpriteEx();
            _landEffectContainer = BitmapDataPool.instance.getAsBitmap("resources/images/landEffectContainer.png");
            _landEffectTF = new TextFieldEx("SFSys-12");
            _landEffectPercentTF = new TextFieldEx("SFSys-12-Right");
            _landEffectPercentTF.width = 50;

            _characterInfoContainer = BitmapDataPool.instance.getAsBitmap("resources/images/characterInfoContainer.png");
            _characterInfoLeftTF = new TextFieldEx("SFSys-12");
            _characterInfoLeftTF.width = 140;
            _characterInfoRightTF = new TextFieldEx("SFSys-12-Right");

            _hpContainer = new SpriteEx();
            _mpContainer = new SpriteEx();

            _enemyInfo = new SpriteEx();
            _enemyInfoContainer = BitmapDataPool.instance.getAsBitmap("resources/images/enemyInfoContainer.png");
            _enemyInfoLeftTF = new TextFieldEx("SFSys-12");
            _enemyInfoLeftTF.width = 140;
            _enemyInfoRightTF = new TextFieldEx("SFSys-12-Right");

            _enemyHPContainer = new SpriteEx();
            _enemyMPContainer = new SpriteEx();


        }

        override protected function cleanView(e:Event):void {
            super.cleanView(e);
        }

        override protected function createView(e:Event):void {
            super.createView(e);


            attachChild(_landEffect, 0, 0, "landEffect");
            _landEffect.attachChild(_landEffectContainer, 0, 0, "landEffectContainer");
            _landEffect.attachChild(_landEffectTF, 16, 14, "landEffectTF");
            _landEffectTF.text = LocalisedMessageEngine.getMessage("LAND_EFFECT");
            _landEffect.attachChild(_landEffectPercentTF, 65, 47, "landEffectPercentTF");

            attachChild(_characterInfoContainer, 224, 0, "characterInfoContainer");
            attachChild(_characterInfoLeftTF, 240, 14, "characterInfoLeftTF");
            attachChild(_characterInfoRightTF, 348, 14, "characterInfoRightTF");
            attachChild(_hpContainer, 274, 32, "hpContainer");
            attachChild(_mpContainer, 274, 48, "mpContainer");

            _enemyInfo.visible = false;
            attachChild(_enemyInfo, 288, 330, "enemyInfo");
            _enemyInfo.attachChild(_enemyInfoContainer, 0, 0, "enemyInfoContainer");
            _enemyInfo.attachChild(_enemyInfoLeftTF, 16, 14, "enemyInfoLeftTF");
            _enemyInfo.attachChild(_enemyInfoRightTF, 60, 14, "enemyInfoRightTF");
            _enemyInfo.attachChild(_enemyHPContainer, 50, 32, "enemyHPContainer");
            _enemyInfo.attachChild(_enemyMPContainer, 50, 48, "enemyMPContainer");

            setCurrentTileInfo(15);
        }

        public function setCurrentCharacter(characterDefinition:CharacterDefinition):void {
            _currentCharacterDefinition = characterDefinition;
            _characterInfoLeftTF.text = _currentCharacterDefinition.name.toUpperCase() + "\n" + LocalisedMessageEngine.getMessage("HP") + "\n" + LocalisedMessageEngine.getMessage("MP");
            _characterInfoRightTF.text = _currentCharacterDefinition.characterClass.toUpperCase() + _currentCharacterDefinition.stats["lv"] + "\n" + _currentCharacterDefinition.stats["hp"] + " / " + _currentCharacterDefinition.stats["maxHp"] + "\n" + _currentCharacterDefinition.stats["mp"] + " / " + _currentCharacterDefinition.stats["maxMp"];

            var hp:int = _currentCharacterDefinition.stats["hp"];
            var maxHp:int = _currentCharacterDefinition.stats["maxHp"];
            var mp:int = _currentCharacterDefinition.stats["mp"];
            var maxMp:int = _currentCharacterDefinition.stats["maxMp"];

            var hpG:BitmapData = new BitmapData(4 + maxHp * 2, 14, true, 0);
            for (var i:int = 2; i <= 2 + maxHp * 2; i++) {
                hpG.setPixel32(i, 0, 0xFFFFFFFF);
                hpG.setPixel32(i, 1, 0xFFFFFFFF);
                hpG.setPixel32(i, 13, 0xFFFFFFFF);
                hpG.setPixel32(i, 14, 0xFFFFFFFF);
            }
            for (i = 2; i <= 12; i++) {
                hpG.setPixel32(0, i, 0xFFFFFFFF);
                hpG.setPixel32(1, i, 0xFFFFFFFF);
                hpG.setPixel32(4 + maxHp * 2, i, 0xFFFFFFFF);
                hpG.setPixel32(3 + maxHp * 2, i, 0xFFFFFFFF);
            }
            hpG.floodFill(2 + maxMp * 2, 4, 0xFFC52121);
            if (hp > 0) {
                if (hp < maxHp) {
                    for (i = 2; i <= 12; i++) {
                        hpG.setPixel32(2 + hp * 2, i, 0xFFE6C542);
                    }
                }

                hpG.floodFill(4, 4, 0xFFE6C542);
            }


            var hpGra:Graphics = _hpContainer.graphics;
            hpGra.clear();
            hpGra.beginBitmapFill(hpG);
            hpGra.drawRect(0, 0, 5 + maxHp * 2, 15);

            if (maxMp != 0) {
                var mpG:BitmapData = new BitmapData(4 + maxHp * 2, 14, true, 0);
                for (i = 2; i <= 2 + maxMp * 2; i++) {
                    mpG.setPixel32(i, 0, 0xFFFFFFFF);
                    mpG.setPixel32(i, 1, 0xFFFFFFFF);
                    mpG.setPixel32(i, 13, 0xFFFFFFFF);
                    mpG.setPixel32(i, 14, 0xFFFFFFFF);
                }
                for (i = 2; i <= 12; i++) {
                    mpG.setPixel32(0, i, 0xFFFFFFFF);
                    mpG.setPixel32(1, i, 0xFFFFFFFF);
                    mpG.setPixel32(4 + maxMp * 2, i, 0xFFFFFFFF);
                    mpG.setPixel32(3 + maxMp * 2, i, 0xFFFFFFFF);
                }
                mpG.floodFill(2 + maxMp * 2, 4, 0xFFC52121);
                if (mp > 0) {
                    if (mp < maxMp) {
                        for (i = 2; i <= 12; i++) {
                            mpG.setPixel32(2 + mp * 2, i, 0xFFE6C542);
                        }
                    }

                    mpG.floodFill(4, 4, 0xFFE6C542);
                }


                var mpGra:Graphics = _mpContainer.graphics;
                mpGra.clear();
                mpGra.beginBitmapFill(mpG);
                mpGra.drawRect(0, 0, 5 + maxMp * 2, 15);
            }
        }

        public function setCurrentEnemy(characterDefinition:CharacterDefinition):void {
            _currentEnemyDefinition = characterDefinition;
            if (characterDefinition == null) {
                _enemyInfo.visible = false;
            }
            else {
                _enemyInfo.visible = true;
                _enemyInfoLeftTF.text = _currentEnemyDefinition.name.toUpperCase() + "\n" + LocalisedMessageEngine.getMessage("HP") + "\n" + LocalisedMessageEngine.getMessage("MP");
                _enemyInfoRightTF.text = "\n" + _currentEnemyDefinition.stats["hp"] + "/ " + _currentEnemyDefinition.stats["maxHp"] + "\n" + _currentEnemyDefinition.stats["mp"] + "/ " + _currentEnemyDefinition.stats["maxMp"];

                var hp:int = _currentEnemyDefinition.stats["hp"];
                var maxHp:int = _currentEnemyDefinition.stats["maxHp"];
                var mp:int = _currentEnemyDefinition.stats["mp"];
                var maxMp:int = _currentEnemyDefinition.stats["maxMp"];

                var hpG:BitmapData = new BitmapData(4 + maxHp * 2, 14, true, 0);
                for (var i:int = 2; i <= 2 + maxHp * 2; i++) {
                    hpG.setPixel32(i, 0, 0xFFFFFFFF);
                    hpG.setPixel32(i, 1, 0xFFFFFFFF);
                    hpG.setPixel32(i, 13, 0xFFFFFFFF);
                    hpG.setPixel32(i, 14, 0xFFFFFFFF);
                }
                for (i = 2; i <= 12; i++) {
                    hpG.setPixel32(0, i, 0xFFFFFFFF);
                    hpG.setPixel32(1, i, 0xFFFFFFFF);
                    hpG.setPixel32(4 + maxHp * 2, i, 0xFFFFFFFF);
                    hpG.setPixel32(3 + maxHp * 2, i, 0xFFFFFFFF);
                }
                hpG.floodFill(2 + maxMp * 2, 4, 0xFFC52121);
                if (hp > 0) {
                    if (hp < maxHp) {
                        for (i = 2; i <= 12; i++) {
                            hpG.setPixel32(2 + hp * 2, i, 0xFFE6C542);
                        }
                    }

                    hpG.floodFill(4, 4, 0xFFE6C542);
                }

                var hpGra:Graphics = _enemyHPContainer.graphics;
                hpGra.clear();
                hpGra.beginBitmapFill(hpG);
                hpGra.drawRect(0, 0, 5 + maxHp * 2, 15);

                if (maxMp != 0) {
                    var mpG:BitmapData = new BitmapData(4 + maxHp * 2, 14, true, 0);
                    for (i = 2; i <= 2 + maxMp * 2; i++) {
                        mpG.setPixel32(i, 0, 0xFFFFFFFF);
                        mpG.setPixel32(i, 1, 0xFFFFFFFF);
                        mpG.setPixel32(i, 13, 0xFFFFFFFF);
                        mpG.setPixel32(i, 14, 0xFFFFFFFF);
                    }
                    for (i = 2; i <= 12; i++) {
                        mpG.setPixel32(0, i, 0xFFFFFFFF);
                        mpG.setPixel32(1, i, 0xFFFFFFFF);
                        mpG.setPixel32(4 + maxMp * 2, i, 0xFFFFFFFF);
                        mpG.setPixel32(3 + maxMp * 2, i, 0xFFFFFFFF);
                    }
                    mpG.floodFill(2 + maxMp * 2, 4, 0xFFC52121);
                    if (mp > 0) {
                        if (mp < maxMp) {
                            for (i = 2; i <= 12; i++) {
                                mpG.setPixel32(2 + mp * 2, i, 0xFFE6C542);
                            }
                        }

                        mpG.floodFill(4, 4, 0xFFE6C542);
                    }


                    var mpGra:Graphics = _enemyMPContainer.graphics;
                    mpGra.clear();
                    mpGra.beginBitmapFill(mpG);
                    mpGra.drawRect(0, 0, 5 + maxMp * 2, 15);
                }
            }

        }

        public function setCurrentTileInfo(tileInfo:int):void {
            _currentTileInfo = tileInfo;
            _landEffectPercentTF.text = _currentTileInfo + "%";
        }

        public function hideLandEffect():void {
            _landEffect.visible = false;
        }

        public function showLandEffect():void {
            _landEffect.visible = true;
        }

        public function moveEnemyInformation(tx:int, ty:int):void {
            _enemyInfo.x = tx;
            _enemyInfo.y = ty;
        }
    }
}
